package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallRecording;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  中心录音查询Action
 * @author yangzj
 * @datetime 2014年10月15日,下午10:33:57
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallCenterRecordingAction extends PublicAction implements ModelDriven<MedicalCallRecording>{
	
	private static final long serialVersionUID = -4164828063263422884L;

	private MedicalCallRecording recording = new MedicalCallRecording();

	/**
	 * @desc 跳转到中心录音查询页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月15日,下午10:47:26
	 */
	@Action(value = "recording_center_init", results = {@Result(location = "recording_center_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询中心电话录音
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月15日,下午11:00:20
	 */
	@Action(value = "recording_center_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getRecordingService().findCenterRecordingByPage(admin, recording, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出中心全部录音
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月15日,下午4:08:51
	 */
	@Action(value = "recording_center_export")
	public String export() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心电话录音明细.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getRecordingService().exportCenterRecording(admin,this.recording);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @desc 导出当前页中心录音
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月15日,下午8:15:49
	 */
	@Action(value = "recording_center_current_export")
	public String currentExport(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心电话录音明细.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getRecordingService().currentExportCenterRecording(admin, recording.getIds());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 批量设置中心录音公开
	 * @return null
	 * @author liy
	 * @datetime 2014年12月26日,上午2:05:30
	 */
	@Action(value = "call_recording_center_open")
	public String open(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.baseAllService.getRecordingService().updateCenterRecordOpen(recording.getIds());
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 批量取消中心录音公开
	 * @return null
	 * @author liy
	 * @datetime 2014年12月26日,上午2:11:03
	 */
	@Action(value = "call_recording_center_cancel")
	public String cancel(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.baseAllService.getRecordingService().updateCenterRecordCancel(recording.getIds());
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	@Override
	public MedicalCallRecording getModel() {
		return recording;
	}
}
